package com.edu.oracle.utils;

import java.sql.*;

public class JdbcUtils {
    //1.创建连接对象
    public static Connection getConnection() throws Exception {

        //1.加载驱动  alt +回车enter
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //2.创建连接对象
        String url ="jdbc:oracle:thin:@//localhost:1521/orcl";
        String user ="scott";//用户
        String pass ="oracle";//密码
        Connection connection= DriverManager.getConnection(url,user,pass);

        return connection;
    }

    //2.关闭资源
    public static void closeResoure(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
        if(resultSet!=null){
            try {
                System.out.println("关闭结果集resultSet");
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(preparedStatement!=null){
            try {
                System.out.println("preparedStatement关闭操作对象");
                preparedStatement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                System.out.println("关闭连接对象");
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        try {
            Connection connection = getConnection();
            System.out.println("抽取的方法连接数据对象"+connection);

            /*PreparedStatement pstm=null;
            ResultSet rs=null;*/

            //调用关闭资源
            closeResoure(connection,null,null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}